﻿@using Coralcode.Base.Entity
@using CoralNode.Core.Entity.Domains.User
@using CoralNode.Core.Entity.Enums
@using CoralNode.MvcClient.Helper
@using Coralcode.Utils.Basic
@using CoralNode.MvcClient.Contexts
@model CoralNode.Core.Entity.Domains.Site.SiteDomain

@{
    var site = Model;
    IList<RoleDomain> roleList = ViewBag.RoleList as IList<RoleDomain>;
    IList<PermissionDomain> rolePermission = ViewBag.RolePermissions as IList<PermissionDomain>;
    IList<PermissionDomain> permissionList = ViewBag.PermissionList as IList<PermissionDomain>;
    int index = -1;
}
  @Html.IncludePage("CoralAdmin/site", "_Left", new { current = "site" })
<style>
    .checkbox_own { margin-top: 12px; }
    .list_authoritylist ul li .checkbox_own { margin-top: 6px; }
</style>
@helper CountSitePermission(IList<PermissionDomain> permissionList) {
    int total = 0;
    foreach (var item in permissionList) {
        if (item.HasChildren == DomainEnums.Binary.True && item.Children != null) {
            foreach (var child in item.Children) {
                total += item.Roles.Count;
            }
        } else {
            total += item.Roles.Count;
        }
    }
    @(total)
}
<div id="pbody">
    <div class="extra">
        <div class="pbody_top">
            <span class="ico ico_setting_system"></span><span class="t t_c">网站设置&nbsp;&nbsp;/&nbsp;&nbsp;<b
                class=" t_small f_no">权限设置(@(Model.Name))</b></span>
        </div>
        <div id="main" class="">
            <div class="local">
                <a href="/m/site" class="ico ico_back btn"></a>
            </div>
            <div class="content_tab extra_normal">
                @Html.IncludePage("CoralAdmin/site", "_navtab", new { current = "permission", siteId = Model.Id })
            </div>
            <div class="userList_cnt">
                <div class="local">
                    <span class="pl_1">设置网站权限</span><span class="info pl_4">
                                                    网站共有<b class="num">@CountSitePermission(rolePermission)</b>
                        个权限</span>
                </div>
                <div class="jurisdiction_cnt">
                    <form id="spform" action="/m/site/EditPermission/@Model.Id" method="post">
                        <div class="cnt">
                            <div class="list_jurisdictionlist">
                                <div class="clearfix list_cnt">
                                    <ul>
                                        @foreach (var role in roleList) {
                                            <li class="more">
                                                @if (rolePermission != null && rolePermission.Count(n => n.Roles.Count(m => m.Id == role.Id) > 0) > 0) {
                                                    <label class="checkbox_own selected">
                                                        <input type="checkbox" checked="checked" /></label>@role.Name
                                                } else {
                                                    <label class="checkbox_own">
                                                        <input type="checkbox" /></label>@role.Name
                                                }
                                                <ul class="d_n">
                                                    @foreach (var item in permissionList) {
                                                        index++;
                                                        if (rolePermission.Count(n => n.Roles.Count(m => m.Id == role.Id) > 0 && n.Id == item.Id) > 0) {
                                                        <li class="more">
                                                            <label class="checkbox_own selected">
                                                                <input type="checkbox" checked="checked" name="rolepers[@index].PermissionId" value="@item.Id"/>
                                                                @Html.Hidden("rolepers[" + index + "].RoleId", role.Id)
                                                            </label>@item.Title
                                                            @if (item.HasChildren == DomainEnums.Binary.True) {
                                                                <ul class="d_n">
                                                                    @foreach (var c in item.Children) {
                                                                        index++;
                                                                        <li>
                                                                            @if (rolePermission.Any(n => n.Roles.Count(m => m.Id == role.Id) > 0 && n.Id == c.Id)) {
                                                                                <label class="checkbox_own selected">
                                                                                    <input type="checkbox" checked="checked" value="@c.Id" name="rolepers[@index].PermissionId"/>
                                                                                    @Html.Hidden("rolepers[" + index + "].RoleId", role.Id)
                                                                                </label>@c.Title
                                                                            } else {
                                                                                <label class="checkbox_own">
                                                                                    <input type="checkbox" value="@c.Id" name="rolepers[@index].PermissionId"/>
                                                                                    @Html.Hidden("rolepers[" + index + "].RoleId", role.Id)
                                                                                </label>@c.Title
                                                                            }
                                                                        </li>
                                                                    }
                                                                </ul>
                                                            }
                                                        </li>
                                                        } else {
                                                        <li class="more">
                                                            <label class="checkbox_own">
                                                                <input type="checkbox"  value="@item.Id" name="rolepers[@index].PermissionId"/>
                                                                @Html.Hidden("rolepers[" + index + "].RoleId", role.Id)
                                                            </label>@item.Title
                                                            @if (item.HasChildren == DomainEnums.Binary.True) {
                                                                <ul class="d_n">
                                                                    @foreach (var c in item.Children) {
                                                                        index++;
                                                                        <li>
                                                                            @if (rolePermission.Count(n => n.Roles.Count(m => m.Id == role.Id) > 0 && n.Id == c.Id) > 0) {
                                                                                <label class="checkbox_own selected">
                                                                                    <input type="checkbox" checked="checked" value="@c.Id" name="rolepers[@index].PermissionId"/>
                                                                                    @Html.Hidden("rolepers[" + index + "].RoleId", role.Id)
                                                                                </label>@c.Title
                                                                            } else {
                                                                                <label class="checkbox_own">
                                                                                    <input type="checkbox" value="@c.Id" name="rolepers[@index].PermissionId"/>
                                                                                    @Html.Hidden("rolepers[" + index + "].RoleId", role.Id)
                                                                                </label>@c.Title
                                                                            }
                                                                        </li>
                                                                    }
                                                                </ul>
                                                            }</li>
                                                        }
                                                    }
                                                </ul>
                                            </li>
                                        }
                                    </ul>
                                </div>
                            </div>
                        </div>
                        <div class="global_fun">
                            <input type="submit" class="btn_2 btn invert" value="提交" />
                            <a href="#" class="js_delete">取消</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
